package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.common.result.Result;
import com.atguigu.srb.common.util.JwtUtils;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.entity.vo.LoginVO;
import com.atguigu.srb.core.pojo.entity.vo.RegisterVO;
import com.atguigu.srb.core.pojo.entity.vo.UserIndexVO;
import com.atguigu.srb.core.pojo.entity.vo.UserInfoDetailVO;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-03-25
 */
@RestController
@RequestMapping("/api/core/userInfo")
//@CrossOrigin
@Api("用户信息管理")
public class ApiUserInfoController {
    @Resource
    UserInfoService userInfoService;

    @ApiOperation("注册")
    @PostMapping("/register/{type}")
    public Result register(
            @RequestBody RegisterVO registerVo,
            @PathVariable("type") Integer type
    ){
        userInfoService.saveUser(registerVo, type);
        return Result.ok().message("注册成功");
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(
            @RequestBody LoginVO loginVo,
            HttpServletRequest request
    ){
        String token = userInfoService.login(loginVo, request);
        return Result.ok().data("token", token);
    }

    @ApiOperation("解析token中userinfo信息")
    @GetMapping("/details")
    public Result details(
            @RequestHeader(value = "token", required = false)
            String token
    ){
        String userId = JwtUtils.getUserId(token);
        UserInfo userInfo = userInfoService.getById(userId);
        System.out.println(userInfo);
        UserInfoDetailVO userInfoDetailVO = new UserInfoDetailVO();
        BeanUtils.copyProperties(userInfo, userInfoDetailVO);
        return Result.ok().data("item", userInfoDetailVO);
    }

    @ApiOperation("判断是否绑定过手机号")
    @GetMapping("/isBindMobile")
    public Result isBindMobile(
            @RequestHeader(value="token", required = false)
            String token
    ){
        Boolean flag = userInfoService.isBindMobile(token);
        return Result.ok().data("flag", flag);
    }

    @ApiOperation("判断手机号是否已被注册")
    @GetMapping("/isUsed/{mobile}")
    public Result isUsed(
            @PathVariable("mobile") String mobile
    ){
        return Result.ok().message(userInfoService.isUsed(mobile).toString());
    }

    @ApiOperation("给wx登录的用户绑定手机号和用户类型")
    @PutMapping("/bindMobile")
    public Result bindMobile(
            @RequestHeader(value = "token", required = false)
            String token,
            @RequestBody RegisterVO registerVO
    ){
        userInfoService.bindMobile(token, registerVO);
        return Result.ok().message("绑定成功");
    }

    @ApiOperation("查询会员个人中心数据")
    @GetMapping("/index")
    public Result index(
            @RequestHeader String token
    ){
        UserIndexVO userIndexVO = userInfoService.getUserIndexVo(token);
        return Result.ok().data("item", userIndexVO);
    }
}